<?php
/**
 * @author yjmp
 * @description : 商家注册页面
 */

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');

$act = !empty($_REQUEST['act'])? $_REQUEST['act'] : 'reg';

/*注册页面显示*/
if ($act == 'reg') {
    //数据获取
    //返点
    $return_rate = $GLOBALS['db']->getOne('SELECT value FROM '.$GLOBALS['ecs']->table('shop_config').' WHERE code = "商家佣金率"');
    if (!empty($return_rate)) {
        $return_rate_format = explode(',',$return_rate); 
        $smarty->assign('return_rate',$return_rate_format);
    }
    $smarty->display('shop_reg.dwt');exit;
}
/*注册事件完成*/
else if ($act == 'commit') {
    /*数据接收*/
    $shop_name = !empty($_POST['shopname'])? trim($_POST['shopname']) : '';
    $shop_addr = !empty($_POST['shopaddr'])? trim($_POST['shopaddr']) : '';
    $Lat = !empty($_POST['Lat'])? trim($_POST['Lat']) : '';
    $Lng = !empty($_POST['Lng'])? trim($_POST['Lng']) : '';
    $tel = !empty($_POST['tel'])? trim($_POST['tel']) : '';
    $phone = !empty($_POST['phone'])? trim($_POST['phone']) : '';
    $rebate = !empty($_POST['rebate'])? trim($_POST['rebate']) : '';
    $password = !empty($_POST['password'])? trim($_POST['password']) : '';
    $shop_desc = !empty($_POST['info'])? trim($_POST['info']) : '';
    /*数据验证*/
    if ($shop_name == '') {
        $content = '商店名不能为空';
        send_err_msg($content);
    }
    if ($shop_addr == '') {
        $content = '商店地址不能为空';
        send_err_msg($content);
    }
    if ($Lat == '') {
        $content = '请选择商店位置';
        send_err_msg($content);
    }
    if ($tel == '') {
        $content = '联系电话不能为空';
        send_err_msg($content);
    }
    if ($phone == '') {
        $content = '商家手机不能为空';
        send_err_msg($content);
    }
    if ($password == '') {
        $content = '密码不能为空';
        send_err_msg($content);
    }
    if ($shop_desc == '') {
        $content = '商家介绍不能为空';
        send_err_msg($content);
    }
    if (!is_reged($tel)) {
        $content = '该手机号码已被使用';
        send_err_msg($content);
    }
    /*数据处理(除了图片)*/
    $pwd_format = md5(md5($password).'10086'); // 密码加密
    /*数据录入*/
    $sql = ' INSERT INTO '.$GLOBALS['ecs']->table('goods').'( goods_name, goods_desc, address, latitude, longitude, tel, return_rate, pwd, is_on_sale, goods_number  )  VALUES ( "'.$shop_name.'", "'.$shop_desc.'", "'.$shop_addr.'", "'.$Lat.'", "'.$Lng.'", "'.$tel.'", "'.$rebate.'", "'.$pwd_format.'", 0, 1 ) ';
    if ($GLOBALS['db']->query($sql)) {
        $content = '申请已发出，请耐心等待审核';
        $type = 'success';
        $link = 'index.php';
        show_yj_message($content,$link,$type);
        exit;
    }
    $content = '添加失败';
    send_err_msg($content);
}

//消息类型测试
else if ($act == 'test') {
    //内容，链接，类型 success, error
    $content = '消息内容测试';
    $type = 'err';
    $link = 'javascript:history.back()';
    show_yj_message($content,$link,$type);
}
/**
 * is registed?
 * @param  string  $tel 商家手机号
 * @return boolean      
 */
function is_reged($tel)
{
    if ($tel != '') {
        $is_reged = $GLOBALS['db']->getOne('SELECT COUNT(*) FROM '.$GLOBALS['ecs']->table('goods').' WHERE tel = "'.$tel.'"');
        return $is_reged? false:true;
    }
    return false;
}
?>
